package com.paradise.ytj.helper;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.paradise.ytj.bean.ContactInfo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by Administrator on 2017/8/15 0015.
 */

public class ContactDao {

    private MyDBOpenHelper dbOpenHelper;

    // 在personDao被new出来的时候 就完成初始化
    public ContactDao(Context context) {
        dbOpenHelper = new MyDBOpenHelper(context);
    }

    /**
     * 往数据库添加一条数据
     */
    public boolean add(String name, String account) {
        boolean result = findByAccount(account);
        if (result)
            return false;
        SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
        if (db.isOpen()) {
            //方法二
            ContentValues values = new ContentValues();
            values.put("name", name);
            values.put("account", account);
            db.insert("contact", null, values);
            // 关闭数据库 释放数据库的链接
            db.close();
        }
        return true;
    }

    /**
     * 查找数据库的操作
     */
    public String getNameByAccount(String account) {
        String name = "未知联系人";
        SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
        if (db.isOpen()) {
            Cursor cursor = db.query("contact", null, "account=?",
                    new String[]{account}, null, null, null);

            //利用游标查询
            if (cursor.moveToFirst()) {
                int index = cursor.getColumnIndex("name"); // 得到phone在表中是第几列
                name = cursor.getString(index);

            }
            // 记得关闭掉 cursor
            cursor.close();
            // 释放数据库的链接
            db.close();
        }
        return name;
    }

    /**
     * 通过账号查找
     *
     * @param account
     * @return
     */
    public boolean findByAccount(String account) {
        SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
        if (db.isOpen()) {
            Cursor cursor = db.query("contact", null, "account=?",
                    new String[]{account}, null, null, null);

            int count = cursor.getCount();
            if (count > 0) {
                return true;
            }
            // 记得关闭掉 cursor
            cursor.close();
            // 释放数据库的链接
            db.close();
        }
        return false;
    }

    /**
     * 删除一条记录
     *
     * @param account
     */
    public void delete(String account) {
        SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
        if (db.isOpen()) {
//            //方法一
//            db.execSQL("iv_delete_normal from contact where name =?",
//                    new Object[]{name});

            //方法二
            db.delete("contact", "account=?", new String[]{account});

            // 释放数据库的链接
            db.close();
        }
    }

    /**
     * 更新一条记录
     */
    public boolean update(String oldAccout, String newname, String newaccount) {
        SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
        int contact = -1;
        if (db.isOpen()) {
//            //方法一
//            db.execSQL("update contact set name=? , account=? where name=?",
//                    new Object[]{newname, newaccount, accout});

            //方法二
            ContentValues values = new ContentValues();
            values.put("account", newaccount);
            values.put("name", newname);
            contact = db.update("contact", values, "account=?", new String[]{oldAccout});
            // 释放数据库的链接
            db.close();
        }
        if (contact > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 查找全部
     */
    public List<ContactInfo> getAllContacts() {
        List<ContactInfo> contacts = null;
        SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
        if (db.isOpen()) {
            contacts = new ArrayList<>();
            Cursor cursor = db.rawQuery("select * from contact ", null);
            while (cursor.moveToNext()) {
                ContactInfo contact = new ContactInfo();
                int nameindex = cursor.getColumnIndex("name");
                int accountindex = cursor.getColumnIndex("account");
                String name = cursor.getString(nameindex);
                String account = cursor.getString(accountindex);
                contact.setName(name);
                contact.setNumber(account);
                contacts.add(contact);
            }
            cursor.close();
            db.close();
        }
        Collections.reverse(contacts);
        return contacts;
    }

    public List<ContactInfo> findByText(String text) {
        List<ContactInfo> contactInfos = new ArrayList<>();
        SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
        if (db.isOpen()) {
            String sql = "select *" + " from " + "contact"
                    + " where " + "account" + " like'%" + text + "%'";
            Cursor cursor = db.rawQuery(sql, null);
            //利用游标查询
            while (cursor.moveToNext()) {
                ContactInfo contactInfo = new ContactInfo();
                int nameIndex = cursor.getColumnIndex("name"); // 得到phone在表中是第几列
                int accountIndex = cursor.getColumnIndex("account");
                String name = cursor.getString(nameIndex);
                String account = cursor.getString(accountIndex);
                contactInfo.setName(name);
                contactInfo.setNumber(account);
                contactInfos.add(contactInfo);
            }
            // 记得关闭掉 cursor
            cursor.close();
            // 释放数据库的链接
            db.close();
        }
        return contactInfos;
    }

}
